/*
 * Copyright (c) 2008 by 1eEurope (Switzerland) AG, Thalwil
 *
 * Customer:	1eEurope (Switzerland) AG
 * Project:		BKS
 * Created:		30.09.2008, WBL
 *
*/

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Xml.Xsl;
using System.IO;
using System.Xml;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web;

namespace By1eEurope.BKS.UserControlZone
{
    /// <summary>
    /// Stores all UserControl-References of the current page in its UserControlZone Field 
    /// </summary>
    public class UserControlZone : BaseFieldControl
    {
        IDictionary<string, string> userControls;
        private IDictionary<string, string> UserControls
        {
            get
            {
                IDictionary<string, string> ret = null;
                if (this.ControlMode == SPControlMode.Edit)
                {
                    if (this.ViewState["UserControls"] == null)
                        this.ViewState["UserControls"] = new Dictionary<string, string>();
                    ret = this.ViewState["UserControls"] as IDictionary<string, string>;
                }
                else
                {
                    ret = userControls;
                }

                return ret;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.ControlMode != SPControlMode.Edit && this.ListItemFieldValue != null)
                this.userControls = ((UserControlZoneValue)this.ListItemFieldValue).Controls;
            else if (this.ControlMode != SPControlMode.Edit)
                this.userControls = new Dictionary<string, string>();
        }

        public override object Value
        {
            get
            {
                UserControlZoneValue val = new UserControlZoneValue();
                val.Controls = (IDictionary<string, string>)this.ViewState["UserControls"];
                return val;
            }
            set
            {
                this.ViewState["UserControls"] = ((UserControlZoneValue)value).Controls;
            }
        }

        public string GetControl(string controlId)
        {
            if (this.UserControls.Keys.Contains(controlId))
                return this.UserControls[controlId];
            return string.Empty;
        }
        public void SetControl(string controlId, string ascxUrl)
        {
            if (this.ControlMode == SPControlMode.Edit)
            {
                this.UserControls.Remove(controlId);
                this.UserControls.Add(controlId, ascxUrl);
            }
        }
        public void RemoveControl(string controlId)
        {
            if (this.ControlMode == SPControlMode.Edit)
                this.UserControls.Remove(controlId);
        }

        private SPListItemCollection availableUserControls;
        public SPListItemCollection AvailableUserControls
        {
            get
            {
                if (availableUserControls == null)
                {
                    string srvRelSiteCollUrl = SPContext.Current.Web.Site.ServerRelativeUrl;
                    if (srvRelSiteCollUrl == "/")
                        srvRelSiteCollUrl = string.Empty;

                    try
                    {
                        using (SPWeb web = SPContext.Current.Site.OpenWeb(srvRelSiteCollUrl + this.WebSiteUrl))
                        {
                            SPList list = web.Lists[this.Field.GetCustomProperty("ListTitle") + ""];
                            this.availableUserControls = list.Items;
                        }
                    }
                    catch (FileNotFoundException exc) { throw new SPException("The properties of the Field 'BKS UserControl Zone' must contain valid values", exc); }
                    catch (ArgumentException exc) { throw new SPException("The properties of the Field 'BKS UserControl Zone' must contain valid values", exc); }
                }
                return this.availableUserControls;
            }
        }
        public string WebSiteUrl
        {
            get
            {
                return this.Field.GetCustomProperty("WebSiteUrl") + "";
            }
        }

        protected override void RenderFieldForDisplay(HtmlTextWriter output)
        {
        }
        protected override void Render(HtmlTextWriter output)
        {
        }
        
    }
}
